﻿using System;
using System.Collections.Generic;
using System.Linq;
using FreshCommonUtility.Dapper;
using FreshCommonUtility.SqlHelper;
using WeChatCmsCommon.EnumBusiness;
using WeChatModel.DatabaseModel;

namespace WeChatDataAccess
{
    public class DailyHistoryData : BaseData<long, DailyHistoryModel>
    {
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="saveModel"></param>
        public long SaveModel(DailyHistoryModel saveModel)
        {
            if (saveModel == null) return 0;
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                if (saveModel.Id < 1)
                {
                    //新增
                    return conn.Insert<long, DailyHistoryModel>(saveModel);
                }

                //修改
                conn.Update(saveModel);
                return saveModel.Id;
            }
        }

        /// <summary>
        /// 根据用户id查询日志记录
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="typeEnum">类型</param>
        /// <param name="dailyYear">年份</param>
        /// <param name="dailyMonth">月份</param>
        /// <param name="dailyDay">天</param>
        /// <returns>查询日志记录</returns>
        public List<DailyHistoryModel> GetDailyHistoryListByUserId(long userId, DailyTypeEnum? typeEnum, int dailyYear, int dailyMonth, int dailyDay)
        {
            List<DailyHistoryModel> dataList;
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                dataList = dailyDay < 1
                    ? conn
                        .GetList<DailyHistoryModel>(new
                        { IsDel = 0, UserId = userId, DailyYear = dailyYear, DailyMonth = dailyMonth })
                        ?.OrderBy(f => f.DailyDate).ToList()
                    : conn
                        .GetList<DailyHistoryModel>(new
                        { IsDel = 0, UserId = userId, DailyYear = dailyYear, DailyMonth = dailyMonth, DailyDay = dailyDay })
                        ?.OrderBy(f => f.DailyDate).ToList();
            }

            if (dataList != null && typeEnum != null)
            {
                return dataList.Where(f => f.DailyType.GetHashCode() == typeEnum.GetHashCode()).ToList();
            }
            else
            {
                return dataList;
            }
        }

        /// <summary>
        /// 获取某人当年剩余调休时间
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public Decimal GetRemainderHistoryByUserId(long userId)
        {
            using (var conn = SqlConnectionHelper.GetOpenConnection())
            {
                var verifyStateList = new List<int>()
                    {VerifyStateEnum.Pass.GetHashCode(), VerifyStateEnum.ReadyDo.GetHashCode()};
                var holidayCountList = conn
                        .GetList<DailyHistoryModel>(new
                        { IsDel = 0, UserId = userId })?.ToList();
                if (holidayCountList == null || holidayCountList.Count < 1)
                {
                    return 0;
                }

                var holidayCount = holidayCountList
                    .Where(f => f.DailyType.GetHashCode() == DailyTypeEnum.Holiday.GetHashCode() && verifyStateList.Contains(f.VerifyState.GetHashCode()))
                    .Sum(r => r.DailyNumber);
                var workCount = holidayCountList
                    .Where(f => f.DailyType.GetHashCode() == DailyTypeEnum.WorkOverTime.GetHashCode() && f.VerifyState.GetHashCode() == VerifyStateEnum.Pass.GetHashCode())
                    .Sum(r => r.DailyNumber);
                return workCount - holidayCount;
            }
        }
    }
}
